﻿@{
    ViewBag.Title = "帐套分配";
    Layout = "~/Views/Shared/_Layout.cshtml";

    List<Maxleaf.EDI.Admin.Models.UserInfoModel> Users = ViewBag.Users as List<Maxleaf.EDI.Admin.Models.UserInfoModel>;
    List<Maxleaf.EDI.Admin.Models.DBInfoModel> DBs = ViewBag.DBs as List<Maxleaf.EDI.Admin.Models.DBInfoModel>;
}

<div class="control-group">
    @using (Ajax.BeginForm("SetDBAccess", "User", new AjaxOptions { HttpMethod = "post", OnSuccess = "onSuccess", OnFailure = "onFailed", OnBegin = "changeBtnState(false)", OnComplete = "changeBtnState(true)" }, new { @class = "form-horizontal", onsubmit="SetHidden();return true;" }))
{
        @Html.Hidden("userName");
        @Html.Hidden("dbIds");
        <div class="table-row">
                        @Html.Label("选择用户名：")
    <select id="UserSelect">
        <option id="optEmpty" value="">
                                
                            </option>
        @foreach (var user in Users)
        {
            <option value="@user.DBIds" >@user.UserName</option>
        }
    </select>

            <input id="regeistBtn" type="submit" value="确定" class="btn btn-primary"/>
             </div>
        
}
        <div class="controls">
             @foreach (var db in DBs)
             {
              <label class="checkbox">
                <input type="checkbox" value="@db.DBId" name='chk_list'>@db.DBName
              </label>
    }
            </div>

 
</div>

@section Scripts {
<script type="text/javascript">
    
    $(document).ready(function(){
        $("#UserSelect").change(function () {
            var data = $("#UserSelect").val().split(",");
            
            var arrChk = $("input[name='chk_list']");
            $(arrChk).each(function () {
                var found=false;
                for (var i = 0; i < data.length; i++) {
                    if (this.value == data[i])
                    {
                        found = true;
                        break;
                    }
                }
                $(this).attr("checked", found);
            });
        });

        
        
    });

    
    function SetHidden() {

        $("#userName").val($("#UserSelect").find("option:selected").text());

        var arrChk = $("input[name='chk_list']");
        var dbIds = "";
        for (var i = 0; i < arrChk.length; i++) {
            if ($(arrChk[i]).attr("checked")) {
                dbIds += $(arrChk)[i].value;
                dbIds += ",";
            }
        }
        if (dbIds.length > 0) { dbIds = dbIds.substring(0, dbIds.length - 1); }
        $("#dbIds").val(dbIds);

    }


    function onSuccess(data) {
        if (data.Success) {
            art.dialog({
                title: '提示信息',
                content: '成功',
                width: 500,
                icon: 'succeed',
                button: [
                    {
                        value: '确定',
                        callback: function () {
                            location.href = '@Url.Action("DBAccess", "User")';
                            }
                        }
                    ]
                });
                } else {
                    onFailed({ "errorMsg": data.Msg });
                }
            }
            function onFailed(err) {
                if (typeof (err.errorMsg) !== "undefined") {
                    err = err.errorMsg;
                } else {
                    err = '失败';
                }
                art.dialog({
                    title: '提示信息',
                    content: err,
                    width: 500,
                    icon: 'succeed',
                    button: [{ value: '确定' }]
                });
            }
            function changeBtnState(state) {
                if (!state) {
                    $("#regeistBtn").attr({ "disabled": true }).val("正在提交");
                    

                } else {
                    $("#regeistBtn").attr({ "disabled": false }).val("确定");
                }
            }
</script>
    }